package com.bootdo.merchant.controller;

import static com.bootdo.common.utils.ShiroUtils.getUser;
import static com.bootdo.common.utils.ShiroUtils.getUserId;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.annotation.IpFilter;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.MD5Utils;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.merchant.domain.MerchantDO;
import com.bootdo.merchant.serivce.MerchantService;

/**
 * @Desc 商户管理
 * @date 2019/11/14
 *
 */
@Controller
@RequestMapping("/merchant/merchant")
public class MerchantController {

    private final String PREFIX = "merchant/merchant";

    @Autowired
    private MerchantService merchantService;

    @GetMapping()
    @RequiresPermissions("merchant:merchant:merchant")
    String merchant(Model model) {
        return PREFIX + "/list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("merchant:merchant:merchant")
    PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<MerchantDO> userList = merchantService.list(query);
        int total = merchantService.count(query);
        PageUtils pageUtils = new PageUtils(userList, total);
        return pageUtils;
    }

    @RequiresPermissions("merchant:merchant:add")
    @Log("添加商户")
    @GetMapping("/add")
    String add(Model model) {
        model.addAttribute("userId", getUserId());
        //获取商户代理信息
        model.addAttribute("agentList", merchantService.queryAgentMerchant());
        return PREFIX + "/add";
    }

    @RequiresPermissions("merchant:merchant:add")
    @Log("保存商户")
    @PostMapping("/save")
    @ResponseBody
    R save(MerchantDO merchant) {
        merchant.setPassword(MD5Utils.encrypt(merchant.getUserName(), merchant.getPassword()));
        if (merchantService.save(merchant) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @RequiresPermissions("merchant:merchant:edit")
    @Log("编辑商户")
    @GetMapping("/edit/{id}")
    String edit(Model model, @PathVariable("id") Long id) {
        MerchantDO merchant = merchantService.get(id);
        model.addAttribute("user", merchant);
        List<Long> list = getUser().getRoleIds();
        boolean f = true;
        for (Long roleId : list){
            if (roleId != 1L && roleId != 64L){
                f = false;
                break;
            }
        }
        model.addAttribute("role", f);
        model.addAttribute("agentList", merchantService.queryAgentMerchant());
        return PREFIX + "/edit";
    }

    @RequiresPermissions("merchant:merchant:edit")
    @Log("修改商户")
    @PostMapping("/update")
    @ResponseBody
    @IpFilter
    R update(MerchantDO merchant) {
        if (merchantService.update(merchant) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @RequiresPermissions("merchant:merchant:editStatus")
    @Log("停用启用商户")
    @PostMapping("/editStatus")
    @ResponseBody
    R editStatus(Long userId, Integer status) {
        if (merchantService.editStatus(userId, status) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @RequiresPermissions("merchant:merchant:resetToken")
    @Log("重置商户令牌")
    @GetMapping("/reset/{id}")
    String reset(Model model, @PathVariable("id") Long id) {
        model.addAttribute("userId", id);
        return PREFIX + "/reset";
    }

    @RequiresPermissions("merchant:merchant:resetToken")
    @Log("重置商户令牌")
    @PostMapping("/resetToken")
    @ResponseBody
    @IpFilter
    R resetToken(Long userId) {
        if (merchantService.resetToken(userId) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @RequiresPermissions("merchant:merchant:lower")
    @Log("启动禁用自动下发")
    @GetMapping("/lower/{id}")
    String lower(Model model, @PathVariable("id") Long id) {
        MerchantDO merchant = merchantService.get(id);
        model.addAttribute("user", merchant);
        return PREFIX + "/lower";
    }

    @RequiresPermissions("merchant:merchant:lower")
    @Log("启动禁用自动下发")
    @PostMapping("/editLower")
    @ResponseBody
    R editLower(MerchantDO merchant){
        if (merchantService.update(merchant) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @RequiresPermissions("merchant:merchant:quota")
    @Log("获取商户流水")
    @GetMapping("/quota/{id}")
    String quota(Model model, @PathVariable("id") Long id) {
        model.addAttribute("id", id);
        return "quota/list";
    }

    @RequiresPermissions("merchant:quota:list")
    @Log("获取商户流水")
    @GetMapping("/quota/page")
    String quota(Model model) {
        model.addAttribute("id", getUserId());
        return "quota/list";
    }
}
